package com.ssyu.examination.system.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ssyu.examination.conmmon.PageModel;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.system.entity.ClazzEntity;
import com.ssyu.examination.system.entity.CourseEntity;

public class ClazzService {
	/**
	 * 分页查询班级信息
	 * 
	 * @param page
	 * @param rows
	 * @param clazz
	 * @param state
	 * @return
	 */
	public PageModel<ClazzEntity> listClzz(int page, int rows, String clazz,
			int state) {
		List<Object> objects = new ArrayList<Object>();

		StringBuilder sb = new StringBuilder();
		sb.append("SELECT c.id,c.name,cs.text as course__text,c.operate_time,c.state,c.memo FROM sys_class c,sys_course cs ");
		sb.append("WHERE c.course_id=cs.id AND c.state=? ");
		objects.add(state);
		if (StringUtils.isNotBlank(clazz)) {
			sb.append("AND c.`name` like ? ");
			objects.add("%" + clazz + "%");
		}
		sb.append(" ORDER BY c.id DESC LIMIT ?,?");
		objects.add((page - 1) * rows);
		objects.add(rows);
		return DBUtil.executeQueryByPage(ClazzEntity.class, sb.toString(),
				objects);
	}

	/**
	 * 班级名称唯一验证
	 * 
	 * @param clazzName
	 * @return
	 */
	public ClazzEntity checkClassName(String clazzName) {
		String sql = "SELECT * FROM sys_class WHERE name=?";
		return DBUtil.getUniqueResult(ClazzEntity.class, sql, clazzName);
	}

	/***
	 * 生成课程树
	 * 
	 * @param id
	 * @return
	 */
	public List<CourseEntity> listMenu(int id) {

		return DBUtil.executeQuery(CourseEntity.class,
				"SELECT * FROM sys_course WHERE pid=?", id);
	}

	/**
	 * 查询课程ID
	 * 
	 * @param couser
	 * @return
	 */
	public CourseEntity getCouserId(String couser) {
		String sql = "SELECT id FROM sys_course WHERE text=?";
		return DBUtil.getUniqueResult(CourseEntity.class, sql, couser);
	}

	/**
	 * 新增班级
	 * 
	 * @param clazzName
	 * @param courseId
	 * @return
	 */
	public int clazzAdd(int operateId, String clazzName, int courseId) {
		String sql = "INSERT INTO sys_class VALUES(NULL,?,?,?,NOW(),1,null)";
		return DBUtil.executeUpdate(sql, clazzName, courseId, operateId);
	}

	/**
	 * 修改班级状态
	 * 
	 * @param id
	 * @param state
	 * @param operateId
	 * @return
	 */
	public int updateClazzState(int id, int state, int operateId) {
		String sql = "UPDATE sys_class SET state=?,operate_user_id=? WHERE id=?";
		return DBUtil.executeUpdate(sql, state, operateId, id);
	}

	/**
	 * 获取指定班级信息
	 * 
	 * @param id
	 * @return
	 */
	public ClazzEntity getClazz(int id) {
		String sql = "SELECT * FROM sys_class WHERE id=?";
		return DBUtil.getUniqueResult(ClazzEntity.class, sql, id);
	}

	/***
	 * 修改班级信息
	 * 
	 * @param id
	 * @param clazzName
	 * @param operateId
	 * @param state
	 * @param courseId
	 * @return
	 */

	public int updateClazz(List<Object> list) {
		String sql = "UPDATE sys_class SET name=?,operate_user_id=?,state=?,course_id=? WHERE id=?";
		return DBUtil.executeUpdate(sql, list);
	}
}
